// pages/seeAlso/seeAlao.js
var api = require('../../utils/api.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
      apipath: api.Apipath,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      var that = this;
      if (wx.getStorageSync('userid')){
        wx.request({
          url: api.CustMessage,
          data: { userId: wx.getStorageSync('userid'), role: wx.getStorageSync('role') },
          method: 'GET',
          success: function (res) {
            console.log(res);
            that.setData({ messageList: res.data.data})
          },
        })
        wx.request({
          url: api.UserInfo,
          data: { userid: wx.getStorageSync('userid'), role: wx.getStorageSync('role')},
          method: 'GET',
          success: function (res) {
            that.setData({ userInfo: res.data.data,})
          },
        })
      } else {
        wx.navigateTo({
          url: '/pages/signin/signin',
        })
      }
    },
    inputContent: function (res) {
      var that = this;
      console.log(res.detail.value);
      that.setData({
        content: res.detail.value
      })
    },
    sendMessage:function(){
      var that = this;
      var content=that.data.content;
      if(content){
        wx.request({
          url: api.SendMessage,
          data: { userId: wx.getStorageSync('userid'), role: wx.getStorageSync('role'), content: content },
          method: 'post',
          success: function (res) {
            console.log(res);
            if(res.data.code==200){
              wx.request({
                url: api.CustMessage,
                data: { userId: wx.getStorageSync('userid'), role: wx.getStorageSync('role') },
                method: 'GET',
                success: function (res) {
                  that.setData({ messageList: res.data.data })
                },
              })
            }else{
              wx.showToast({
                title: '发送失败',
                icon: 'none',
                duration: 1000
              });
              return;
            }
          },
        })
      }else{
        wx.showToast({
          title: '发送内容不能为空',
          icon: 'none',
          duration: 1000
        });
        return;
      }
      console.log(content);
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})